/**
 * @param {Object} preYearData
 * @param {Object} nowYearData
 * 计算单前值
 */
function cul(code){
	let   preYear =  new Date().getFullYear()-1;
	
	
	let now,datePre ;
	if(new Date().getFullYear()==(preYear+1)){
		now = new Date();
		datePre = new Date((preYear+1)+'-01-01');
	}else{
		now = new Date((preYear+1)+'-12-31');
		datePre = new Date((preYear+1)+'-01-01');
	}
	
	datePre = new Date(datePre).Format("yyyy-MM-dd");
	now = now.Format("yyyy-MM-dd");
	let preYearData = GuVo.getDateRang(code,preYear+"-01-01",preYear+"-12-31");
	let dayDao = GuVo.getDateRang(code, datePre, now);
	let preYearMonth = getLastMonth2(code);
	
	Promise.all([dayDao,preYearData,preYearMonth]).then(res=>{
		let nowYearResult = compareNowYear(res[0].data[code].day);
		let preYearResult = comparePreYear(res[1].data[code].day);
	    compareEveryYear(res[2],code,(res=>{
			nowYearResult.code=code;
			computResult(nowYearResult,preYearResult,res);
		}));
		
	});
}

//和当年做比较
function compareNowYear(arr){
	let nowPrice = parseFloat(arr[arr.length-1][1]);
	let result = getMinMaxAvg(arr,2,3);
    result.nowPrice = nowPrice;
	return result;
}
//和上年做比较
function comparePreYear(arr){

	//时间，开盘价，收盘价，最高价，最低价，成交量
	let result = getMinMaxAvg(arr,3,4);
	return result;
}
//统计每年
function compareEveryYear(res,code,callBack){

	var arr = res.split("\n");
	arr.pop();
	arr.shift();
	arr = arr.map(row => {
		return row.split(" ");
	})
	let year = arr[0][0].substr(0,2);
	let year1 = year.substr(0,1);
	if(year1=="0" || year1=="1" || year1=="2"){
		year="20"+year;
	}else{
		year="19"+year;
	}
	year = parseInt(year);
	let pList = [];
	let now = new Date().getFullYear();
    for(let i = year;i<now;i++){
		pList.push(GuVo.getDateRang(code,year+"-01-01",year+"-12-31"));
    	year++;
	}
    let yearMaxMin={};
	
	Promise.all(pList).then(res=>{
		let arrData = [];
		res.map(item=>{
			arrData = arrData.concat(item.data[code].day);
			let result = compareNowYear(item.data[code].day);
			let monthMax = "max"+result.maxTime.substr(5,2);
			let monthMin = "min"+result.minTime.substr(5,2);
			if(yearMaxMin[monthMax]){
				yearMaxMin[monthMax].push(result);
			}else {
				yearMaxMin[monthMax] = [result];
			}
			if(yearMaxMin[monthMin]){
				yearMaxMin[monthMin].push(result);
			}else {
				yearMaxMin[monthMin] = [result];
			}
		});
		
		let maxMinAvg = getMinMaxAvg(arrData,3,4);
		
		let total = pList.length;
		let month = new Date().Format("yyyy-MM-dd").substr(5,2);
		let maxPersent = 0;
		let minPersent = 0; 
		for(let key in yearMaxMin){
			if(key.indexOf("max")!=-1 && key.substr(3,2)==month){
				maxPersent = yearMaxMin[key].length/total;
			}
			if(key.indexOf("min")!=-1 && key.substr(3,2)==month){
				minPersent = yearMaxMin[key].length/total;
			}
		}
		callBack({maxPersent:maxPersent,minPersent:minPersent,yearMaxMin:yearMaxMin,allyear:maxMinAvg});

	});

}

function computResult(now,pre,every){
	let buy = buyScore(now,pre,every);
	let sell = sellScore(now,pre,every);

	if(app.culBuySell){
		app.culBuySell({buy:buy,sell:sell,code:now.code,max:every.allyear.max,min:every.allyear.min,now:now,pre:pre});
	}
	console.log(buy+":"+sell)
}

//avg,price,max
//(price-avg/max-avg)
function computPersentSell(a,b,c){
	let p = (b-a)/(c-a);
	p = p>1?1:(p<0?0:p);
	return p;
}
//min,price,avg
// avg-price/avg-min
function computPersentBuy(a,b,c){
	let p = (c-b)/(c-a);
	p=p>1?1:(p<0?0:p);
	return p;
}
/**
 * @param {Object} now
 * @param {Object} pre
 * @param {Object} every
 * 数值=当年靠近最小值比率*40+上年靠近最小值比率*30+当月占每年低位月份比率*30
 */
function buyScore(now,pre,every){
	let nowPrice = now.nowPrice;
	let pNow = computPersentBuy(now.min,nowPrice,now.avg);
	let pPre = computPersentBuy(pre.min,nowPrice,pre.avg);
	let score = pNow*30+pPre*40+every.minPersent*30;
	return score;

}
/**
 * @param {Object} now
 * @param {Object} pre
 * @param {Object} every
 * 计算卖出公式
 * 数值=当年靠近最大值比率*40+上年靠近最大值比率*30+当月占每年高位月份比率*30
 */
function sellScore(now,pre,every){
	let nowPrice = now.nowPrice;
	let pNow = computPersentSell(now.avg,nowPrice,now.max);
	let pPre  = computPersentSell(pre.avg,nowPrice,pre.max);
	let score = pNow*40+pPre*30+every.maxPersent*30;
	return score;
}

//获取最大最小平均值
function getMinMaxAvg(arr,highIdx,lowIdx){
		let max=0;
		let min=99999999;
		let avg = 0;
		let maxTime = "";
		let minTime = "";
		arr.forEach(res=>{
			let r3 = parseFloat(res[highIdx]);
			let r4 = parseFloat(res[lowIdx]);
			max = r3>max?r3:max;
			min =r4<min?r4:min;
			maxTime = max==r3?res[0]:maxTime;
			minTime = min==r4?res[0]:minTime;
		})
		max=parseFloat(max);
		min=parseFloat(min);
		avg = (max+min)*0.5;
		avg = parseFloat(avg.toFixed(2));
		return {maxTime:maxTime,minTime:minTime,max:max,min:min,avg:avg};
}